package Diagnosis

import (
	"MedicalTreatment/global"
	model "MedicalTreatment/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type UploadResult struct {
	ResultId int
	DrugId   []int
	Number   []int
}
type UploadResults struct {
	gorm.Model
	ResultId int
	DrugId   int
	Number   int
}

func Update(r *gin.Engine) {
	r.POST("/updateResult", func(context *gin.Context) {
		var upload UploadResult
		if err := context.ShouldBindJSON(&upload); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		fmt.Println(upload)
		if err := global.DB.Debug().Unscoped().Where("result_id = ?", upload.ResultId).Delete(&model.DrugList{}).Error; err != nil {
			fmt.Println("111", err)

			context.JSON(200, gin.H{
				"Msg": err,
			})
		}
		for i := 0; i < len(upload.DrugId); i++ {
			creatAssociation := UploadResults{
				ResultId: upload.ResultId,
				DrugId:   upload.DrugId[i],
				Number:   upload.Number[i],
			}
			if err := global.DB.Table("drug_lists").Debug().Create(&creatAssociation).Error; err != nil {
				fmt.Println("113", err)
				context.JSON(200, gin.H{
					"Msg": err,
				})
			}
			fmt.Println("123")
		}

		context.JSON(200, gin.H{
			"Msg": "编辑成功",
		})
	})
}
